from flask import Flask, render_template, request
from flask_restful import Resource, Api
from vars import surround_config
from passwd import dbuser, dbpass, dbhost, dbport
from pyspark.sql import SQLContext, SparkSession
from pyspark.sql import functions as F
import random
import re

app = Flask(__name__)
api = Api(app)

class Test_surround(Resource):
    def post(self):
        spark = SparkSession. \
            Builder(). \
            appName('sql'). \
            master('local'). \
            getOrCreate()
        # mysql 配置(需要修改)
        prop = {'user': dbuser, 
                'password': dbpass,
                'driver': 'com.mysql.jdbc.Driver'}
        # database 地址(需要修改)
        url = 'jdbc:mysql://' + dbhost + ':' + dbport + '/ratemap'
        # 读取表，data1是用户表，data2是business表
        data1 = spark.read.jdbc(url=url, table='user', properties=prop)
        data2 = spark.read.jdbc(url=url, table='business', properties=prop)


        args = request.json

        print('===get args: {}'.format(args))
    
        ### 用户输入的x坐标
        x = float(args['latitude'])
        ### 用户输入的y坐标
        y = float(args['longitude'])
        ### 附近坐标差
        size = 1
        ### 用户要求的评级
        star = float(args['rating'])
        ### 用户的交通工具
        tool = args['transportation']
        ### 用户是否携带孩子
        with_children = args['with_children']
        ### 存放所有符合要求的可停放自行车的店铺
        bikes = []
        ### 存放所有符合要求的可停放汽车的店铺
        cars = []
        ### 保存所有满足评级、并且在用户附近的数据
        closer = []
        ### 保存最后要返回的数据
        data = []
        ### 获取满足用户评级要求的数据
        info = data2.filter(data2.stars > star).collect()
        ### 对满足评级要求的店铺进行循环遍历
        for item in info:
            ### 根据商店的经纬度字段进行判断，如果在用户附近
            if(item.latitude > x - size and item.latitude < x + size and item.longitude > y - size and item.longitude < y + size):
                ### 将该店铺的数据加入数组closer中
                closer.append(item)
        ### 如果用户的工具是bike
        if tool == 'bike':
            for close in closer:
                ### 查询可以停放bike的店铺
                if close.bike_parking == 'True':
                    ### 将该店铺的数据加入bikes数组中
                    bikes.append(close)
            ### 再对bikes数组进行循环遍历
            for b in bikes:
                ### 如果用户携带孩子
                if with_children == 'True':
                    ### 检查good_for_kids字段，再加入数组
                    if b.good_for_kids == 'True':
                        data.append(b)
                ### 如果不携带孩子
                else:
                    ### 不需要考虑，直接加入数组
                    data.append(b)
        # 同理，如果用户的工具是car
        if tool == 'car':
            for close in closer:
                park = close.business_parking
                if park == '0' or park == 'None' or park == 'False':
                    continue
                else:
                    if 'True' in park:
                        cars.append(close)
            for c in cars:
                if with_children == 'True':
                    if c.good_for_kids == 'True':
                        data.append(c)
                else:
                    data.append(c)
        return data
        # 关闭spark会话
        spark.stop()

api.add_resource(Test_surround, '/surround')

if __name__ == '__main__':
    app.run(debug=True, host=surround_config['host'], port=surround_config['port'])